package com.SouthernWall_404.AgeRight.Common.Event;

import com.SouthernWall_404.AgeRight.Common.API.Capability.Missions;
import com.SouthernWall_404.AgeRight.Common.API.Capability.Provider.MissionsProvider;
import com.SouthernWall_404.AgeRight.Common.API.MissionAPI.*;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.ArrayList;
import java.util.List;

public class MissionEventHandler {

    
    @SubscribeEvent
    public static void onPlayerJoin(PlayerInteractEvent.RightClickItem event) {
        // 确保只处理玩家实体，并且只在服务器端执行
        if (event.getLevel().isClientSide() || !(event.getEntity() instanceof Player)) {
            return;
        }
        
        Player player = (Player) event.getEntity();
        
        // 延迟执行，确保玩家能力已加载
        event.getLevel().getServer().execute(() -> {
            player.getCapability(MissionsProvider.MISSIONS_CAPABILITY).ifPresent(missions -> {
                // 检查是否已经有任务，避免重复添加
                    addInitialZombieMission(missions, player);
            });
        });
    }
    
    private static void addInitialZombieMission(Missions missions, Player player) {
        // 创建击杀僵尸的子任务
        KillMissionInfo zombieMission = new KillMissionInfo(1, EntityType.ZOMBIE);
        KillMissionInfo skeletonMission = new KillMissionInfo(1, EntityType.SKELETON);

        
        // 创建奖励（例如：一些经验值和铁锭）
        List<ItemStack> rewards = new ArrayList<>();
        rewards.add(new ItemStack(Items.IRON_INGOT, 3));
        rewards.add(new ItemStack(Items.EXPERIENCE_BOTTLE, 1));
        RewardInfo rewardInfo = new RewardInfo(rewards);
        
        // 创建子任务列表
        List<AbstractMissionInfo> subMissions = new ArrayList<>();
        subMissions.add(zombieMission);
        subMissions.add(skeletonMission);
        
        // 创建主任务
        MainMissionInfo mainMission = new MainMissionInfo(rewardInfo, subMissions,"gui.mission.main.test");
        
        // 添加任务到玩家
        missions.addMission(mainMission,player);
    }
}